<?php
$ci_aquisicao_item = @$_POST['ci_aquisicao_item'];
if(!$ci_aquisicao_item)
	exit;

require_once('../includes/frameworkajax.php');

//Extensões conhecidas pelo sistema
$exts = array('avi' => '<i class="fa fa-file-video-o fa-5x text-danger"></i>', 'doc' => '<i class="fa fa-file-text fa-5x text-danger"></i>', 'docx' => '<i class="fa fa-file-word-o fa-5x text-danger"></i>', 
				'gif' => '<i class="fa fa-file-image-o fa-5x text-danger"></i>', 'jpg' => '<i class="fa fa-file-image-o fa-5x text-danger"></i>', 'mov' => '<i class="fa fa-file-video-o fa-5x text-danger"></i>', 
				'mp3' => '<i class="fa fa-file-audio-o  fa-5x text-danger"></i>', 'mpeg' => '<i class="fa fa-file-video-o"></i>', 'no' => '<i class="fa fa-file-o"></i>', 
				'odb' => '<i class="fa fa-file-text fa-5x text-danger"></i>', 'odf' => '<i class="fa fa-file-text fa-5x text-danger"></i>', 'odg' => '<i class="fa fa-file-text fa-5x text-danger"></i>', 
				'ods' => '<i class="fa fa-file-excel-o fa-5x text-danger"></i>', 
				'odt' => '<i class="fa fa-file-text fa-5x text-danger"></i>', 'pdf' => '<i class="fa fa-file-pdf-o fa-5x text-danger"></i>', 
				'png' => '<i class="fa fa-file-image-o fa-5x text-danger"></i>', 'ppt' => '<i class="fa fa-file-powerpoint-o fa-5x text-danger"></i>', 
				'pptx' => '<i class="fa fa-file-powerpoint-o fa-5x text-danger"></i>', 
				'rar' => '<i class="fa fa-file-archive-o fa-5x text-danger"></i>', 'txt' => '<i class="fa fa-file-text-o fa-5x text-danger"></i>', 
				'wmv' => '<i class="fa fa-file-video-o fa-5x text-danger"></i>', 'xls' => '<i class="fa fa-file-excel-o fa-5x text-danger"></i>', 
				'xlsx' => '<i class="fa fa-file-excel-o fa-5x text-danger"></i>', 'zip' => '<i class="fa fa-file-archive-o fa-5x text-danger"></i>');

//Seleciona os arquivos de um fluxo
$sqlFiles = "select ci_file,
	cd_aquisicao,
	nm_file,
	ds_ext,
	ds_descricao,
	to_char(dt_data, 'DD/MM/YYYY') as data,
	cd_aquisicao_item
from aquisicao.tb_file
where cd_aquisicao_item=$ci_aquisicao_item
order by ci_file asc";
$files = Connection::query($sqlFiles);

if($files->rowCount() > 0){
	while ($rowItem = $files->fetch()) {

		if(@$exts[$rowItem['ds_ext']]){
			$ext = $rowItem['ds_ext'];
		}
		else{
			$ext = 'no';
		}

		$ds_descricao = '';
		
		if($rowItem['ds_descricao']){
			$ds_descricao = 'Descrição: '.$rowItem['ds_descricao'];
		}
		
		$shortName = $rowItem['nm_file'];
		
		if(strlen($rowItem['nm_file']) > 50)
			$shortName = substr($rowItem['nm_file'], 0, 50).'...';
			
		$nameIconWindows = wordwrap($rowItem['nm_file'], 18, '<br>', true);

		if(strlen($rowItem['nm_file']) > 30)
			$shotName = substr($rowItem['nm_file'], 0, 30).'...'; 
		
		echo '<div class="fileicon" data-toggle="tooltip" data-placement="top" title="Data da Criação: '.$rowItem['data'].$ds_descricao.' Extensão: '.$rowItem['ds_ext'].'"> 
			<a href="down.php?ci_file='.$rowItem['ci_file'].'&cd_aquisicao='.$rowItem['cd_aquisicao'].'&ds_ext='.$rowItem['ds_ext'].'&nm_file='.$rowItem['nm_file'].'">'.$exts[$rowItem['ds_ext']].'<br>'.$nameIconWindows.'</a>
			<div class="control">				
				<button type="button" class="btn btn-success" title="Editar" onclick="openDialogFilesEdit('.$rowItem['ci_file'].', \''.$rowItem['nm_file'].'\', \''.trim($rowItem['ds_descricao']).'\')"><i class="fa fa-pencil"></i></button>
				<button type="button" class="btn btn-danger" title="Apagar" onclick="openDialogFilesDel(\''.$rowItem['ci_file'].'.'.$rowItem['ds_ext'].'\', \''.$shortName.'\')"><i class="fa fa-trash"></i></button>
			</div>
		  </div>';
	}
}else{
	echo 'Nenhum arquivo foi anexado.';
}
?>
<script type="text/javascript">
$(function(){
	$('[data-toggle="tooltip"]').tooltip();
	$(".fileicon").mouseover(function(){
	    $(this).find(".control").show();
	}).mouseout(function(){
	    $(this).find(".control").hide();
	}).css({
		"float":"left", "margin":"5px 10px 10px 5px", "min-width":"99px", "max-width":"105px", "width":"100%", "min-height": "100px", "max-height": "140px", "height": "100%", "word-wrap": "break-word"
	}).find(".control").hide();

	/*$('.fileicon a[title], .fileicon input[title]').tooltipsy({
	    show: function (e, $el) {
	        $el.css({
	            'left': parseInt($el[0].style.left.replace(/[a-z]/g, '')) - 50 + 'px',
	            'opacity': '0.0',
	            'display': 'block'
	        }).animate({
	            'left': parseInt($el[0].style.left.replace(/[a-z]/g, '')) + 50 + 'px',
	            'opacity': '1.0'
	        }, 300);
	    },
	    hide: function (e, $el) {
	        $el.slideUp(100);
	    }
	});*/
	
});
</script>